package freespiritsocial.io.user.util

import android.app.Activity
import android.graphics.Rect
import com.previewlibrary.GPreviewBuilder
import freespiritsocial.io.user.data.UserViewInfo
import freespiritsocial.io.user.ui.fragment.UserFragment

class PreViewUtils(val context: Activity,val imageList: ArrayList<UserViewInfo>) {


    fun preImage(it: String?,index: Int) {
        GPreviewBuilder.from(context)
            .setData(imageList)
            .setUserFragment(UserFragment::class.java)
            .setCurrentIndex(getThumbPosition(it,index))
            .setSingleFling(true)
            .setType(GPreviewBuilder.IndicatorType.Number)
            .start()
    }

    fun setBounds(index: Int,bounds: Rect) {
        imageList[index].bounds = bounds
    }

    fun setBounds(bounds: Rect) {
        for (userViewInfo in imageList) {
            userViewInfo.bounds = bounds
        }
    }


    private fun getThumbPosition(filename: String?,index: Int): Int {
        imageList.forEachIndexed { i,userViewInfo ->
            if ((userViewInfo.url.contains(filename ?: "") || userViewInfo.videoUrl?.contains(
                    filename ?: ""
                ) == true) && (userViewInfo.index == index || userViewInfo.hashCode == index)
            ) {
                return i
            }
        }
        return 0
    }
}